package com.doctor.ui.homedoctor;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.doctor.adapter.DiseaseRecordAdapter;
import com.doctor.bean.DiseaseRecordBean;
import com.doctor.bean.PatientFileBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.MyApplication;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseActivity;
import com.doctor.ui.LoginActivity;
import com.doctor.ui.R;
import com.doctor.view.NoScrollListView;

public class AnswerPatientPhoneActivity extends BaseActivity{

	/**
	 * 接电话病人信息界面
	 */
	
	private PatientFileBean bean;
	private NoScrollListView listView;
	private DiseaseRecordAdapter adapter;
	private List<DiseaseRecordBean> beanList;
	private TextView exitBtn;
	private TextView enterBtn;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.answer_phone_patient_detail);
		
		bean=(PatientFileBean) getIntent().getSerializableExtra(ConstConfig.BEAN);
		initiaView();
	}
	
	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		TextView num=(TextView) findViewById(R.id.number);
		num.setText(bean.getId()+"");
		
		TextView name=(TextView) findViewById(R.id.name);
		name.setText(bean.getName());
		
		TextView email=(TextView) findViewById(R.id.email);
		email.setText(bean.getEmail());
		
		TextView sex=(TextView) findViewById(R.id.sex);
		sex.setText(bean.getSex());
		
		TextView birthDay=(TextView) findViewById(R.id.birthday);
		birthDay.setText(bean.getBirthDay());
		
		TextView endDay=(TextView) findViewById(R.id.endday);
		endDay.setText(bean.getEndDay());
		
		TextView address=(TextView) findViewById(R.id.address);
		address.setText(bean.getAddress());
		
		TextView phone=(TextView) findViewById(R.id.phone);
		phone.setText(bean.getPhone());
		
		TextView mobile=(TextView) findViewById(R.id.mobile);
		mobile.setText(bean.getMobile());
		
		TextView body=(TextView) findViewById(R.id.body);
		body.setText(bean.getBody());
		
		TextView bloodType=(TextView) findViewById(R.id.bloodType);
		bloodType.setText(bean.getBloodType());
		
		TextView familyHistory=(TextView) findViewById(R.id.familyHistory);
		familyHistory.setText(bean.getFamilyHistory());
		
		TextView personalHistory=(TextView) findViewById(R.id.personalHistory);
		personalHistory.setText(bean.getPersonalHistory());
		
		TextView diseaseHistory=(TextView) findViewById(R.id.diseaseHistory);
		diseaseHistory.setText(bean.getDiseaseHistory());
		
		exitBtn=(TextView) findViewById(R.id.exitBtn);
		enterBtn=(TextView) findViewById(R.id.enterBtn);
		if(MyApplication.getInstance().getUserId()>=0){
			//已登录
			exitBtn.setEnabled(false);
			enterBtn.setEnabled(false);
			exitBtn.setVisibility(View.INVISIBLE);
			enterBtn.setVisibility(View.INVISIBLE);
		} else {
			//未登录
			exitBtn.setOnClickListener(click);
			enterBtn.setOnClickListener(click);
		}
		
		listView=(NoScrollListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				DiseaseRecordBean diseaseRecordBean=(DiseaseRecordBean) arg1.getTag(R.id.bean_tag);
				Intent intent=new Intent(AnswerPatientPhoneActivity.this,AnswerPoneDiseaseActivity.class);
				intent.putExtra(ConstConfig.BEAN, diseaseRecordBean);
				startActivity(intent);
			}
		});
		
		new Thread() {
			public void run() {
				beanList=DbOperator.getInstance(AnswerPatientPhoneActivity.this).selectDiseaseRecordData(bean.getId());
				runOnUiThread(new Runnable() {
					public void run() {
						if (beanList!=null && beanList.size()>0) {
							adapter=new DiseaseRecordAdapter(AnswerPatientPhoneActivity.this, beanList);
							listView.setAdapter(adapter);
							/**
							 * 设置ListView高度自适应
					         * ListView item必须是LinearLayout
							 * 其他的Layout(如RelativeLayout)没有重写onMeasure()，所以会在onMeasure()时抛出异常
							*/
							int height=0;
							for (int i = 0; i < adapter.getCount(); i++) {
								View listItem=adapter.getView(i, null, listView);
								listItem.measure(0, 0);
								height+=listItem.getMeasuredHeight();
							}
							ViewGroup.LayoutParams params=listView.getLayoutParams();
							params.height=height+(listView.getDividerHeight()*(adapter.getCount()-1));
							listView.setLayoutParams(params);
						}
					}
				});
			}
		}.start();
	}
	
	android.view.View.OnClickListener click=new View.OnClickListener() {		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if (arg0==exitBtn) {
				finish();
			} else if (arg0==enterBtn) {
				Intent intent=new Intent(AnswerPatientPhoneActivity.this,LoginActivity.class);
				startActivity(intent);
				finish();
			}
		}
	};
	
	
}
